<template>
  <div class="login-container">
    <!-- 登录页 -->
    <el-form v-if="show" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <span class="title login">登录</span>
        <span class="title" @click="show=false">注册</span>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;height:50px;font-size:20px;" @click.native.prevent="handleLogin">登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>

    </el-form>
    <!-- 注册页 -->
    <el-form v-else ref="regreg" :model="registForm" :rules="registRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <span class="title" @click="show=true">登录</span>
        <span class="title login">注册</span>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="registForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="registForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="email">
        <span class="svg-container">
          <i class="el-icon-mobile-phone" style="color:black" />
        </span>
        <el-input
          ref="email"
          v-model="registForm.email"
          placeholder="验证邮箱"
          name="email"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="phone">
        <span class="svg-container">
          <i class="el-icon-mobile-phone" style="color:black" />
        </span>
        <el-input
          ref="phone"
          v-model="registForm.phone"
          placeholder="电话号码"
          name="phone"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;height:50px;font-size:20px;" @click.native.prevent="handleRegist">注册</el-button>

    </el-form>
  </div>
</template>

<script>
import { user, reg, login } from '@/api/user'
import router from '@/router'
// import { validUsername } from '@/utils/validate'

export default {
  data() {
    const yanzhengUsername = (rule, value, callback) => {
      user().then(res => {
        if (res.data.find(v => v.username === value
        )) {
          callback(new Error('该用户名已存在'))
        } else {
          callback()
        }
      })
    }

    return {
      show: true,
      // 登录数据
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ min: 2, max: 10, message: '用户名长度不得大于2位小于10位', trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,

      // 注册数据
      registForm: {
        username: '',
        password: '',
        email: '',
        phone: ''
      },
      registRules: {
        username: [{ required: true, trigger: 'blur', validator: yanzhengUsername }
        ],
        password: [{ required: true, min: 6, max: 16, trigger: 'blur', message: '请输入6-16位' }]
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleRegist() {
      this.$refs.regreg.validate((valid) => {
        if (valid) {
          reg(this.registForm).then(res => {
            this.show = true
            alert('注册成功!')
          })
        } else {
          alert('请输入正确格式!')
          return false
        }
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          login(this.loginForm).then(res => {
            if (res.data[0]) {
              this.$message.success('登录成功')
              window.localStorage.setItem('user', res.data[0].id)
              router.push('/dashboard')
            } else {
              this.loginForm.password = ''
              this.$message.error('请确认账号密码是否驶入正确')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
      font-size: 18px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #fff;
    height: 50px;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url(../../assets/login/u1.jpg) no-repeat center center;
  // background-color: $bg;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-form {
    // position: relative;
    width: 520px;
    max-width: 100%;
    // padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    display: flex;
    height: 80px;
    margin-bottom: 20px;
    justify-content: space-between;
    align-items: center;
    .login {
      border-bottom: 2px solid blue;
    }
    .title {
      display: block;
      width: 200px;
      height: 100%;
      line-height: 100px;
      font-size: 26px;
      color: $light_gray;
      text-align: center;
      font-weight: 400;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .validate {
    width: 518px;
    background-color: transparent;
    ::v-deep .el-form-item__content {
      width: 518px;
      display: flex;
      justify-content: space-between;
      .validate_input {
      background-color: #fff;
      width: 400px !important;
      border-radius: 5px;
      input {
        padding-left: 40px;
        // width: 200px !important;
      }
      // .el-button {
      //   border-radius: 10px;
      // }
    }
    }
  }
}
</style>
